'use strict';

const Service = require('egg').Service;
var moment = require('moment');

const {
    ERROR,
    SUCCESS,
} = require('../util/util');

class TopicService extends Service {
    async selectTopics(
        isMy,
        uid,
        departureTime, 
        topicType, 
        cityType,
        limit, 
        offset) {

            const { ctx, logger } = this

            try {
                
                var where = {
                    state: 1,
                }

                var order = [['departureTime', 'ASC']]
                // 根据入参设置where参数

                if (isMy === true) { //查询自己的帖子 不需要departureTime的筛选
                    where["uid"] = uid/1
                    order = [['created_at', 'DESC']]

                    if (departureTime) {
                        where = {
                            departureTime: {
                                $gte: stDate
                            }
                        }
                    }
                } else {
                    // 入参有出发时间按照出发时间筛选 如果没有 按照当前时间
                    if (departureTime) {
                        var stDate = moment(departureTime).format('YYYY-MM-DD HH:mm:ss')
                        // var endDate = moment(departureTime).add(1, 'd').format('YYYY-MM-DD HH:mm:ss')

                        where = {
                            departureTime: {
                                // $between: [stDate, endDate],
                                $gte: stDate
                            }
                        }
                    }else {
                        var stDate = moment(Date()).format('YYYY-MM-DD HH:mm:ss')
                        where = {
                            departureTime: {
                                $gte: stDate
                            }
                        }
                    }
                }

                // 城市类型
                if (cityType) {
                    where["cityType"] = cityType
                }
                // 帖子类型
                if (topicType) {
                    where["topicType"] = topicType
                }
                
                const topics = await ctx.model.Topic.findAndCountAll({
                    include: [
                        {
                            model: ctx.model.Config,
                            as: 'cityConfig'
                        }
                    ],
                    where: where,
                    order: order,
                    limit: limit,
                    offset: offset
                })
            
                return Object.assign(SUCCESS(), {
                    data: Object.assign(topics),
                });
            } catch (error) {
                logger.error(error)
                return Object.assign(ERROR(), {
                    msg: '服务器错误',
                });
            }
    }

    async addTopic(
        uid,
        departureTime, 
        topicType, 
        cityType,
        peopleNum,
        remark,
        linkman,
        linkPhone) {

            const { ctx, logger } = this
            
            try {
                // 查发帖人uid是否正常
                const user =  await ctx.model.User.findOne({
                    where: {
                        uid: uid/1
                    }
                })

                // user不存在或者状态不正常报错
                if (!user || user.state !== 1) {
                    return Object.assign(ERROR(), {
                        msg: 'user不存在或state不正常',
                    });
                }

                const result =  await ctx.model.Topic.create({
                    uid,
                    departureTime,
                    topicType, 
                    cityType,
                    peopleNum,
                    remark,
                    linkman,
                    linkPhone
                })

                return Object.assign(SUCCESS(), {
                    msg: '发布成功',
                });
            } catch (error) {
                logger.error(error)
                return Object.assign(ERROR(), {
                    msg: '服务器错误',
                });
            }
    }
}

module.exports = TopicService;
